package com.hzit.order.controller;

import cn.hutool.core.util.StrUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.hzit.common.utils.PayAsyncVo;
import com.hzit.common.utils.R;
import com.hzit.common.utils.TbPayLog;
import com.hzit.order.entity.Cart;
import com.hzit.order.entity.TbAddress;
import com.hzit.order.entity.TbOrder;
import com.hzit.order.service.AddressService;
import com.hzit.order.service.OrderService;
import com.hzit.order.service.PayLogSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.print.DocFlavor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Controller
public class OrderController {
	@Autowired
	private AddressService addressService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private PayLogSerivce payLogSerivce;
	//1. 默认欢迎页面
	@GetMapping({"/","/getOrderInfo.html"})
	public String index(Model model, HttpSession session){
		//1.1 得到当前登录用户名
		String name = (String) session.getAttribute("name");
		if(StrUtil.isBlank(name)){      // 如果用户名不存在就登录
			return "redirect:http://auth.hzit.com/gitee/login?returnUrl=http://order.hzit.com";
		}
		//1.2 根据用户名查询地址列表
		List<TbAddress> addressList = addressService.findAddresListByName(name);
		//1.3 将用户对应的地址列表放到model中
		model.addAttribute("addressList",addressList);
		//1.4 查询出购物车列表
		List<Cart> cartList = orderService.findCartList(name);
		//1.5 放到model中
		model.addAttribute("cartList",cartList);
		return "getOrderInfo";
	}

	//2. 添加订单
	@PostMapping("/order/add")
	public String add(TbOrder order, HttpSession session,Model model){
		//2.1 得到当前登录用户名
		String name = (String) session.getAttribute("name");
		//2.2 添加到订单
		orderService.add(name,order);

		//2.3 从redis中得到支付日志信息
		TbPayLog payLog = payLogSerivce.getPayLogFromRedis(name);
		//2.4 将支付日志放到model中
		model.addAttribute("payLog",payLog);
		return "pay";
	}

	//3. 只保存tb_order表一张表的数据
	@PostMapping("/order/save")
	@ResponseBody
	public R saveOrder(){
		orderService.saveOrder();
		return R.ok();
	}

	//4. 生成支付下单页面
	@GetMapping("/order/createPayPage")
	@ResponseBody
	public String createPayPage(String outTradeNo,String totalFee) throws AlipayApiException {
		String result = orderService.createPayPage(outTradeNo,totalFee);
		return result;
	}

	//5. 进行同步回调处理:
	//业务：如果支付成功后，就展示当前用户的订单列表
	@GetMapping("/order/returnUrl")
	public String orderReturnUrl(HttpSession session,Model model){
		//5.1 得到当前登录用户名
		String name = (String) session.getAttribute("name");
		//5.2 根据当前用户的登录名，查询出此用户的订单列表
		List<TbOrder> orderList = orderService.findOrderListByname(name);
		//5.3 查询出订单及订单项列表放到model中
		model.addAttribute("orderList",orderList);
		//5.4 到达订单列表
		return "orderlist";
	}

	//6. 进行异步回调处理
	//业务：如果支付成功，就修改支付日志及修改订单状态
	@PostMapping("/order/notifyUrl")
	@ResponseBody
	public String ordernotifyUrl(PayAsyncVo vo, HttpServletRequest request) throws UnsupportedEncodingException, AlipayApiException {
		//1. 获取支付宝POST过来反馈信息
		Map<String,String[]> requestParams = request.getParameterMap();
		String result = orderService.sendNotify(requestParams,vo);
		return result;
	}
}
